/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Portions created by the Initial Developer are
 * Copyright (C) 2012 the Initial Developer.
 * All Rights Reserved.
 *
 * The Original Code is "http://semi-frame.googlecode.com/svn/trenk/".
 *
 * The Initial Developer of the Original Code is the author below.
 */
/**
 * @file Logger.h
 * @brief auto generated.
 * @author mornongcloud
 * @date 2011/10/15
 */

#ifndef SEMILOGGLOGGER_H_
#define SEMILOGGLOGGER_H_

#include "semi/conf/Configurator.h"
#include "semi/logg/LogPrinter.h"
#include "semi/util/LogUtil.h"
#include <execinfo.h>

using namespace semi;

namespace semi {
namespace logg {

const std::string LV_NM_FATAL = "FATAL";
const std::string LV_NM_ERROR = "ERROR";
const std::string LV_NM_WARN = "WARN";
const std::string LV_NM_INFO = "INFO";
const std::string LV_NM_DEBUG = "DEBUG";
const std::string LV_NM_TRACE = "TRACE";

/*
 * @brief Logger class.
 *
 */
class Logger {
public:

	Logger();
	virtual ~Logger();

	static Logger getLogger(std::string logname);

	static Logger getCurrentLogger();

	/**
	 * @brief Available for logging.
	 *
	 * @return Available
	 */
	bool isDebugEnabled();

	/**
	 * @brief Available for logging.
	 *
	 * @return Available
	 */
	bool isInfoEnabled();

	/**
	 * @brief Available for logging.
	 *
	 * @return Available
	 */
	bool isTraceEnabled();

	/**
	 * @brief Available for logging.
	 *
	 * @return Available
	 */
	bool isWarnEnabled();

	/**
	 * @brief Logging.
	 *
	 * @param msg Log message
	 * @param func __func__
	 * @param file __FILE__
	 * @param line __LINE__
	 */
	void trace(std::string msg, std::string func = "", std::string file = "",
			int line = 0);

	/**
	 * @brief Logging.
	 *
	 * @param msg Log message
	 * @param func __func__
	 * @param file __FILE__
	 * @param line __LINE__
	 */
	void debug(std::string msg, std::string func = "", std::string file = "",
			int line = 0);

	/**
	 * @brief Logging.
	 *
	 * @param msg Log message
	 * @param func __func__
	 * @param file __FILE__
	 * @param line __LINE__
	 */
	void info(std::string msg, std::string func = "", std::string file = "",
			int line = 0);

	/**
	 * @brief Logging.
	 *
	 * @param msg Log message
	 * @param func __func__
	 * @param file __FILE__
	 * @param line __LINE__
	 */
	void warn(std::string msg, std::string func = "", std::string file = "",
			int line = 0);

	/**
	 * @brief Logging.
	 *
	 * @param msg Log message
	 * @param func __func__
	 * @param file __FILE__
	 * @param line __LINE__
	 */
	void error(std::string msg, std::string func = "", std::string file = "",
			int line = 0);

	/**
	 * @brief Logging.
	 *
	 * @param msg Log message
	 * @param func __func__
	 * @param file __FILE__
	 * @param line __LINE__
	 */
	void fatal(std::string msg, std::string func = "", std::string file = "",
			int line = 0);

	void configureLevel(std::string strLevel);

	std::string toString();
	static std::string toStringAll();

	bool releaseLock();

	static void releaseLockAll();

	// log pool
	typedef std::map<std::string, Logger> LoggerMap;
	static Logger::LoggerMap loggerPool;
	static Logger addLogger(std::string logname, Logger loggr);

	// @brief log name.
	std::string Name;
	// @brief log printer.
	LogPrinter printer;

	/**
	 * @brief Logging.
	 *
	 * @param msg Log message
	 * @param func __func__
	 * @param file __FILE__
	 * @param line __LINE__
	 *
	 * @return Log message string.
	 */
	static std::string createMessage(std::string msg, std::string func,
			std::string file, int line);

private:

	// @brief log level.
	int loglevel;
	// @brief identifier on semi.conf
	std::string confName;

	static int LV_TRACE;
	static int LV_DEBUG;
	static int LV_INFO;
	static int LV_WARN;
	static int LV_ERROR;
	static int LV_FATAL;

	static Logger current;

};

}
}

#endif /* SEMILOGGLOGGER_H_ */
